import pygame
import random
import os
import sys

# 初始化 Pygame
pygame.init()

# 设置窗口大小和标题
SCREEN_WIDTH, SCREEN_HEIGHT = 480, 720
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("aircraft-war")

# 加载图像资源
ASSET_DIR = ""

def load_image(name, scale=1.0):
       path = os.path.join(os.path.dirname(__file__), name)
       image = pygame.image.load(path).convert_alpha()
       # 可选：按比例缩放图片
       size = image.get_size()
       size = (int(size[0] * scale), int(size[1] * scale))
       return pygame.transform.scale(image, size)


PLAYER_IMG = load_image("player.svg", 0.5)
ENEMY_IMG = load_image("enemy.svg", 0.3)
BULLET_IMG = load_image("bullet.svg", 0.3)
EXPLOSION_IMG = load_image("explosion.svg", 0.3)

# 加载音效
BULLET_SOUND = pygame.mixer.Sound(os.path.join(ASSET_DIR, "sound.wav"))

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# 玩家类
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = PLAYER_IMG
        self.rect = self.image.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT-60))
        self.speed = 5

    def update(self, keys):
        if keys[pygame.K_LEFT] and self.rect.left > 0:
            self.rect.x -= self.speed
        if keys[pygame.K_RIGHT] and self.rect.right < SCREEN_WIDTH:
            self.rect.x += self.speed
        if keys[pygame.K_UP] and self.rect.top > 0:
            self.rect.y -= self.speed
        if keys[pygame.K_DOWN] and self.rect.bottom < SCREEN_HEIGHT:
            self.rect.y += self.speed

# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = BULLET_IMG
        self.rect = self.image.get_rect(center=(x, y))
        self.speed = -10

    def update(self, *args, **kwargs):
        self.rect.y += self.speed
        if self.rect.bottom < 0:
            self.kill()

# 敌机类
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = ENEMY_IMG
        self.rect = self.image.get_rect(x=random.randint(0, SCREEN_WIDTH-40), y=-40)
        self.speed = random.randint(2, 5)

    def update(self, *args, **kwargs):
        self.rect.y += self.speed
        if self.rect.top > SCREEN_HEIGHT:
            self.kill()

# 爆炸动画类
class Explosion(pygame.sprite.Sprite):
    def __init__(self, center):
        super().__init__()
        self.image = EXPLOSION_IMG
        self.rect = self.image.get_rect(center=center)
        self.frame = 0
        self.last_update = pygame.time.get_ticks()
        self.frame_rate = 50

    def update(self, *args, **kwargs):
        now = pygame.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.frame += 1
            self.last_update = now
        if self.frame >= 3:
            self.kill()

# 创建精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

# 游戏主循环参数
running = True
clock = pygame.time.Clock()
score = 0
font = pygame.font.SysFont("simhei", 30)  # 使用黑体字体显示中文
last_enemy = pygame.time.get_ticks()
enemy_delay = 1000  # 每秒生成一个敌机

def draw_text(surf, text, size, x, y):
    text_surface = font.render(text, True, WHITE)
    surf.blit(text_surface, (x, y))

# 主循环
while running:
    clock.tick(60)
    now = pygame.time.get_ticks()

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bullet = Bullet(player.rect.centerx, player.rect.top)
                all_sprites.add(bullet)
                bullets.add(bullet)
                BULLET_SOUND.play()

    # 生成敌人
    if now - last_enemy > enemy_delay:
        enemy = Enemy()
        all_sprites.add(enemy)
        enemies.add(enemy)
        last_enemy = now

    # 更新逻辑
    keys = pygame.key.get_pressed()
    all_sprites.update(keys)

    # 子弹击中敌机
    hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
    for hit in hits:
        score += 1
        explosion = Explosion(hit.rect.center)
        all_sprites.add(explosion)

    # 敌机撞到玩家
    if pygame.sprite.spritecollideany(player, enemies):
        running = False

    # 绘制画面
    screen.fill(BLACK)
    all_sprites.draw(screen)
    draw_text(screen, f"得分: {score}", 30, 10, 10)
    pygame.display.flip()

# 结束界面
draw_text(screen, "游戏结束", 40, SCREEN_WIDTH//2 - 60, SCREEN_HEIGHT//2)
pygame.display.flip()
pygame.time.wait(2000)

pygame.quit()
sys.exit()
